package com.simlink.sns.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.simlink.sns.model.Event;
import com.simlink.sns.model.User;
import com.simlink.sns.service.FeedService;
import com.simlink.sns.service.UserService;
import com.simlink.sns.util.ShiroUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zhuzhe
 * @date 2018/6/5 14:43
 * @email 1529949535@qq.com
 */
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private FeedService feedService;
	
	@RequiresPermissions("user:add")
	@PostMapping("/add")
	public String add(User user) {
		log.info(user.toString());
		return "add-ok";
	}

	@RequiresPermissions("user:update")
	@PostMapping("/update")
	public String update(User user) {
		log.info(user.toString());
		return "update-ok";
	}

	@RequiresPermissions("user:delete")
	@GetMapping("/delete/{id}")
	public String add(@PathVariable("id") Integer id) {
		log.info(id.toString());
		return "delete-ok";
	}

	@RequiresPermissions("user:query")
	@GetMapping("/list")
	public String getAll() {
		User user = ShiroUtils.getUser();
		return "getAll-ok";
	}

	@GetMapping("/counter")
	public Map<String, Long> getCounter() {
		return userService.getCounterOfFollowAndShortPost(ShiroUtils.getUserId());
	}
	
	@GetMapping("/feeds")
	public List<Event> getFeeds() {
		return feedService.getFeeds(ShiroUtils.getUserId());
	}
	
	@PostMapping("/get")
	public User getCurrentUser() {
		User user = ShiroUtils.getUser();
		user.setCounter(userService.getCounterOfFollowAndShortPost(ShiroUtils.getUserId()));
		return user;
	}
}
